#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Instruments/Bonds/FixedRateBond.h>
#pragma unmanaged 
#include <ql\instruments\bonds\btp.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Instruments { namespace Bonds {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IBTP
	public ref class CBTP  : 
            public CFixedRateBond,
            public Cephei::QL::Instruments::Bonds::IBTP
	{
	protected: 
		boost::shared_ptr<QuantLib::BTP>* _ppBTP;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::BTP>* _phBTP;
#endif
		Object^ _BTPOwner;     // reference to object that manages the storage for this object
	internal:
		CBTP (DateTime maturityDate, Double fixedRate, Double redemption, Microsoft::FSharp::Core::FSharpOption<DateTime>^ startDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Cephei::QL::IPricingEngine^ QL_Pricer);
		CBTP (DateTime maturityDate, Double fixedRate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ startDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Cephei::QL::IPricingEngine^ QL_Pricer);
        CBTP (boost::shared_ptr<QuantLib::BTP>& childNative, Object^ owner);
        CBTP (QuantLib::BTP& childNative, Object^ owner);
        CBTP (CBTP^ copy);
        CBTP (PLATFORM::Type^ t);
#ifdef STRUCT
        CBTP (QuantLib::BTP childNative);
#endif       
#ifdef HANDLE
		CBTP (QuantLib::Handle<QuantLib::BTP>& childNative, Object^ owner);
		CBTP (QuantLib::Handle<QuantLib::BTP> childNative);
#endif
		virtual ~CBTP ();
		!CBTP ();

	internal:
		QuantLib::BTP& GetReference ();
		boost::shared_ptr<QuantLib::BTP>& GetShared ();
		QuantLib::BTP* GetPointer ();
        void SetBTP (boost::shared_ptr<QuantLib::BTP> native)
        {
            if (_ppBTP != NULL)
                delete _ppBTP;
            _ppBTP = new boost::shared_ptr<QuantLib::BTP> (native);
            SetFixedRateBond (boost::dynamic_pointer_cast<QuantLib::FixedRateBond> (*_ppBTP));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::BTP>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
		virtual Double AccruedAmount (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d) ;
		virtual Double Yield (Double cleanPrice, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxEvaluations) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IBTP Vector interface
	ref class CBTPVector sealed : 
							    public Core::INativeVector<IBTP^>,
							    public Core::INativeVector<Core::Generic::ICoCell<IBTP^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::BTP>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::BTP>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::BTP>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CBTPVector ();
#ifdef STRUCT
		CBTPVector (std::vector<QuantLib::BTP>& vec, Object^ owner);
		CBTPVector (std::vector<QuantLib::BTP> vec);
#endif       
		CBTPVector (std::vector<boost::shared_ptr<QuantLib::BTP>>& vec, Object^ owner);
#ifdef HANDLE
		CBTPVector (std::vector<QuantLib::Handle<QuantLib::BTP>>& vec, Object^ owner);
#endif
		virtual ~CBTPVector ();
		!CBTPVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IBTP^ default[int] 
		{
			virtual IBTP^ get (int index) = Core::INativeVector<IBTP^>::default::get;
			virtual void set (int index, IBTP^ value) = Core::INativeVector<IBTP^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Core::Generic::ICoCell<Cephei::QL::Instruments::Bonds::IBTP^>^ CItem[int] 
		{
			virtual Core::Generic::ICoCell<IBTP^>^ get (int index) = Core::INativeVector<Core::Generic::ICoCell<IBTP^>^>::default::get;
			virtual void set (int index, Core::Generic::ICoCell<IBTP^>^ value) = Core::INativeVector<Core::Generic::ICoCell<IBTP^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::BTP>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::BTP>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::BTP>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Instruments::Bonds::IBTP^>::typeid)]
	[FactoryFor(Cephei::QL::Instruments::Bonds::IBTP::typeid)]
	[FactoryFor(Cephei::QL::Instruments::Bonds::IBTP_Factory::typeid)]
	public ref class CBTP_Factory sealed : public IBTP_Factory
	{
	public:
        virtual IBTP^ Create (DateTime maturityDate, Double fixedRate, Double redemption, Microsoft::FSharp::Core::FSharpOption<DateTime>^ startDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Cephei::QL::IPricingEngine^ QL_Pricer);
        virtual IBTP^ Create (DateTime maturityDate, Double fixedRate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ startDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ issueDate, Cephei::QL::IPricingEngine^ QL_Pricer);
        /* <summary> Create a hybrid Vector of IBTP, with event notification of changes </summary> */
        virtual Cephei::Core::IVector<IBTP^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IBTP, with event notification of changes </summary> */
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IBTP^>^>^>^ CreateCellVector();
        virtual Cephei::Core::IVector<IBTP^>^ CreateVector(PLATFORM::Collections::Generic::IEnumerable<IBTP^>^ source);
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<IBTP^>^>^>^ CreateCellVector(PLATFORM::Collections::Generic::IEnumerable<Core::Generic::ICoCell<IBTP^>^>^ source);
		virtual Core::INativeVector<Cephei::QL::Instruments::Bonds::IBTP^>^ CreateNativeVector ()
		{
			return REFNEW CBTPVector ();
		}

        virtual Core::INativeMatrix<Cephei::QL::Instruments::Bonds::IBTP^>^ CreateNativeMatrix ()
		{
			return nullptr;
		}

        virtual Core::INativeCube<Cephei::QL::Instruments::Bonds::IBTP^>^ CreateNativeCube ()
		{
			return nullptr;
		}
    };
   
/*Cephei*/ } /*QL*/ } /*Instruments*/ } /*Bonds */}
